package com.aibabelx.springbootdemo.demos.web.controller;

import com.aibabelx.springbootdemo.demos.web.entity.User;
import com.aibabelx.springbootdemo.demos.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  //返回json
@RequestMapping("/user")   //加前缀，这下面的所有接口都要加/user前缀
public class UserController {
    @Autowired
    private UserService userService;

    //插入和修改操作  ， 需要外界发送数据，用post
    @PostMapping("/save")
    public boolean save(@RequestBody User user) {
        return userService.saveOrUpdate(user);
    }

    //查询所有数据
    @GetMapping("/findAll")
    public List<User> findAll() {
        return userService.list();
    }

    //x修改
    @DeleteMapping("/{id}")
    public User update(@PathVariable Integer id, String name) {
        //要先查表然后做保存处理
        User user = userService.getBaseMapper().selectById(id);
        user.setName("修改后的为aii" + name);
        userService.save(user);
        return user;
    }

    //删除
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id) {
        return userService.removeById(id);
    }

}
